package com.lsh.io;

import java.io.*;
import java.time.Duration;
import java.time.Instant;

/**
 * @author ：LiuShihao
 * @date ：Created in 2021/3/3 11:09 上午
 * @desc ：  只要是处理纯文本数据，就优先考虑使用字符流。除此之外都使用字节流。
 *
 */
public class FileIO {
    public static File testTxt = new File("/Users/LiuShihao/IdeaProjects/netty_demo/src/main/resources/test.txt");
    public static File catImg = new File("/Users/LiuShihao/IdeaProjects/netty_demo/src/main/resources/cat.jpg");


    public static void main(String[] args) throws Exception {
        Instant now = Instant.now();
        System.out.println("开始复制："+now);
//        copyFile();
//        copyByReaderAndWriter1();
        copyByReaderAndWriter2();

        Instant end = Instant.now();
        // Duration  期间Instant          Period  时期  LocalDateTime
        System.out.println("复制完成："+end+"，耗时："+ Duration.between(now,end));


    }

    /**
     * 使用FileinputStream、FileOutputStream   与原文件一样
     * 将一个文件复制一份
     */
    public static void  copyFile() throws Exception {
//        File file = new File("/Users/LiuShihao/IdeaProjects/netty_demo/src/main/resources/test.txt");
        File file_copy1 = new File("/Users/LiuShihao/IdeaProjects/netty_demo/src/main/resources/test_copy.txt");
//        File file_copy1 = new File("/Users/LiuShihao/IdeaProjects/netty_demo/src/main/resources/cat_copy.jpg");
        FileInputStream fis = new FileInputStream(testTxt);
        FileOutputStream fos = new FileOutputStream(file_copy1);
        //byte[] bytes = new byte[1024];
        byte[] bytes = new byte[fis.available()];
        int read = fis.read(bytes);
        if (read != -1){
            fos.write(bytes);
        }
        fis.close();
        fos.close();
    }

    /**
     * 字符流
     * 使用FileInputStream、FileOutputStream、InputStreamReader、OutputStreamWriter、BufferedReader、BufferedWriter 复制文件
     *
     * readLine() 不用while的判断只会输出一行。
     */
    public static void copyByReaderAndWriter1() throws Exception {
//        File testTxt = new File("/Users/LiuShihao/IdeaProjects/netty_demo/src/main/resources/test.txt");
        File file_copy2 = new File("/Users/LiuShihao/IdeaProjects/netty_demo/src/main/resources/test_copy2.txt");
        FileInputStream fis = new FileInputStream(testTxt);
        FileOutputStream fos = new FileOutputStream(file_copy2);
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fis));
        BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(fos));
        String line;
        while ((line = bufferedReader.readLine()) != null){
            System.out.println(line);
            bufferedWriter.write(line);
        }
        // 注意： fis、fos要在bufferedWriter、bufferedReader关闭之后，否则会报错！
        bufferedWriter.close();
        bufferedReader.close();
        fis.close();
        fos.close();
    }

    /**
     * 字符流
     * 使用BufferedReader、BufferedWriter、FileReader、FileWriter复制文件
     * @throws Exception
     */
    public static void copyByReaderAndWriter2() throws Exception{
        File file_copy3 = new File("/Users/LiuShihao/IdeaProjects/netty_demo/src/main/resources/test_copy3.txt");
        BufferedReader bufferedReader = new BufferedReader(new FileReader(testTxt));
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file_copy3));
        //readLine() 每次读取一行
        String line;
        while ((line = bufferedReader.readLine()) != null){
            System.out.println(line);
            bufferedWriter.write(line);
        }
        bufferedWriter.close();
        bufferedReader.close();
    }

}
